package admin

import (
	ct "context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	. "xm_shop/models"
	pb "xm_shop/proto/rbacAccess"
)

type AccessController struct{}

// 获取顶级模块
func (ac AccessController) GetModule(context *gin.Context) {
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessGet(ct.Background(), &pb.AccessGetRequest{})
	context.JSON(http.StatusOK, gin.H{
		"accessList": res.GetAccessList(),
	})
}

// 增加权限
func (ac AccessController) Add(context *gin.Context) {
	moduleName := strings.Trim(context.PostForm("module_name"), " ")
	accessType, err1 := strconv.Atoi(context.PostForm("type"))
	url := strings.Trim(context.PostForm("url"), " ")
	moduleId, err2 := strconv.Atoi(context.PostForm("module_id"))
	sort, err3 := strconv.Atoi(context.PostForm("sort"))
	description := strings.Trim(context.PostForm("description"), " ")
	status, err4 := strconv.Atoi(context.PostForm("status"))
	actionName := strings.Trim(context.PostForm("action_name"), " ")
	if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}

	if moduleName == "" {
		context.String(http.StatusBadRequest, "模块名称不能为空")
		return
	}
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessAdd(ct.Background(), &pb.AccessAddRequest{
		ModuleName:  moduleName,
		ActionName:  actionName,
		Type:        int64(accessType),
		Url:         url,
		ModuleId:    int64(moduleId),
		Sort:        int64(sort),
		Description: description,
		Status:      int64(status),
	})
	context.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}

// 获取顶级模块和子模块(获取所有权限)
func (ac AccessController) GetModuleAndChild(context *gin.Context) {
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessGet(ct.Background(), &pb.AccessGetRequest{})
	context.JSON(http.StatusOK, gin.H{
		"accessList": res.GetAccessList(),
	})
}

// 获取权限的详细信息
func (ac AccessController) GetOnlyAccess(context *gin.Context) {
	accessId, err := strconv.Atoi(context.Param("id"))
	if err != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessGet(ct.Background(), &pb.AccessGetRequest{
		Id: int64(accessId),
	})
	context.JSON(http.StatusOK, gin.H{
		"accessList": res.AccessList[0],
	})
}

// 编辑权限
func (ac AccessController) Edit(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	moduleName := strings.Trim(context.PostForm("module_name"), " ")
	accessType, err2 := strconv.Atoi(context.PostForm("type"))
	url := strings.Trim(context.PostForm("url"), " ")
	moduleId, err3 := strconv.Atoi(context.PostForm("module_id"))
	sort, err4 := strconv.Atoi(context.PostForm("sort"))
	description := strings.Trim(context.PostForm("description"), " ")
	status, err5 := strconv.Atoi(context.PostForm("status"))
	actionName := strings.Trim(context.PostForm("action_name"), " ")
	if err1 != nil || err2 != nil || err3 != nil || err4 != nil || err5 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	if moduleName == "" {
		context.String(http.StatusBadRequest, "模块名称不能为空")
		return
	}
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessEdit(ct.Background(), &pb.AccessEditRequest{
		Id:          int64(id),
		ModuleName:  moduleName,
		ActionName:  actionName,
		Type:        int64(accessType),
		Url:         url,
		ModuleId:    int64(moduleId),
		Sort:        int64(sort),
		Description: description,
		Status:      int64(status),
	})
	context.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}

// 删除权限
func (ac AccessController) Delete(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
	}
	rbacClient := pb.NewRbacAccessService("rbac", MicroClient)
	res, _ := rbacClient.AccessDelete(ct.Background(), &pb.AccessDeleteRequest{
		Id: int64(id),
	})
	context.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}
